package com.klhappy.houyuan.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

import com.klhappy.houyuan.R;
import com.klhappy.houyuan.activity.RatingActivity;
import com.klhappy.houyuan.bean.EvaluateBean;
import com.klhappy.houyuan.bean.EvaluateOrder.ItemEvaBean;
import com.klhappy.houyuan.bean.GoodsAllBean;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.dialog.Photograph;
import com.klhappy.houyuan.utils.NotifyMethod;
import com.klhappy.houyuan.utils.imageselector.Bimp;
import com.klhappy.houyuan.utils.imageselector.RatingGridActivity;
import com.klhappy.houyuan.view.ImageManager;

import static com.klhappy.houyuan.activity.RatingActivity.*;

public class OrderItemAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<EvaluateBean.OrderDetail> orderDetail;
    private List<ItemEvaBean> lists = new ArrayList<>();
    private Photograph photograph;
    private int num;
    private DisplayMetrics dm;
    private GridAdapter adapter;

    public OrderItemAdapter(Context context, ArrayList<EvaluateBean.OrderDetail> orderDetail, DisplayMetrics dm) {
        this.context = context;
        this.orderDetail = orderDetail;
        this.dm = dm;
        for (int i = 0; i < orderDetail.size(); i++) {
            ItemEvaBean evaBean = new ItemEvaBean();
            lists.add(evaBean);
        }
    }

    public OrderItemAdapter(Context context) {
        this.context = context;
    }

    public void Notif(){
        notifyDataSetChanged();
    }

    public int getNum() {
        return num;
    }

    public List<ItemEvaBean> getLists() {
        return lists;
    }

    @Override
    public int getCount() {
        return orderDetail.size();
    }

    @Override
    public Object getItem(int position) {
        return orderDetail.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        OrderItemViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.rating_item, null);
            holder = new OrderItemViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (OrderItemViewHolder) convertView.getTag();
        }

        ImageManager.loadImage(UrlConstant.BASE_URL + orderDetail.get(position).getG_icon(), holder.ibImage);
        holder.tvDes.setText(orderDetail.get(position).getG_name());
        holder.tvPrice.setText("￥" + orderDetail.get(position).getG_price() + "/" + orderDetail.get(position).getG_unit());
        holder.tvCount.setText("x" + orderDetail.get(position).getNo());

        holder.freshBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                lists.get(position).setRating(rating + "");
            }
        });

        holder.tv_evaluation.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                lists.get(position).setCount(s.toString());
            }
        });

        holder.gv_img.setSelector(new ColorDrawable(Color.TRANSPARENT));
        holder.gv_img.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
                num = position;
                if (i == Bimp.allListBitmap.get(position).size()) {
                    photograph = new Photograph(context) {
                        //相册
                        @Override
                        public void doGoToImg() {
                            this.dimiss();
                            Intent intent = new Intent(context, RatingGridActivity.class);
                            Bimp.max = 0;//重置为0，以便刷新
                            intent.putExtra("number", num);
                            context.startActivity(intent);
                        }

                        //拍照
                        @Override
                        public void doGoToPhone() {
                            this.dimiss();
                            NotifyMethod.getInstance().postNotification(AVG);
                        }
                    };
                    photograph.show();
                }
            }
        });
        adapter = new GridAdapter(context, dm, Bimp.allListBitmap.get(position));
        holder.gv_img.setAdapter(adapter);
        return convertView;
    }

    static class OrderItemViewHolder {
        @InjectView(R.id.iv_image)
        ImageView ibImage;
        @InjectView(R.id.tv_des)
        TextView tvDes;
        @InjectView(R.id.tv_price)
        TextView tvPrice;
        @InjectView(R.id.tv_count)
        TextView tvCount;
        @InjectView(R.id.freshBar)
        RatingBar freshBar;
        @InjectView(R.id.tv_evaluation)
        EditText tv_evaluation;
        @InjectView(R.id.gv_img)
        GridView gv_img;

        public OrderItemViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }

}